(function($){
	
	$.getURLParams = function(url) {
		if (!url) url = window.location.href;
		var paramIndex = url.indexOf("?");
		var params = {};
		if (paramIndex > -1) {
			var paramArray = url.substr(paramIndex + 1).split("&");
			$.each(paramArray, function() {
				var keyVal = this.split("=");
				if (keyVal[1])
					params[keyVal[0]] = keyVal[1];
			});
		}
		return params;
	};
	
	$.replaceURLParams = function(arg0, arg1) {
		var url = window.location.href;
		var args = arg0 || {};
		if (typeof arg0 === "string") {
			url = arg0;
			args = arg1;
		}
		var paramIndex = url.indexOf("?");
		if (paramIndex > -1) {
			args = $.extend($.getURLParams(url), args);
			url = url.substring(0, paramIndex);
		}
		var argsArray = [];
		for (var key in args) {
			argsArray.push(key + "=" + args[key]);
		}
		var result = url;
		if (argsArray.length > 0)
			result = url + "?" + argsArray.join("&");
		return result;
	};
	
	$.getHash = function(key) {
		var val = "";
		var hash = window.location.hash;
		if (hash && hash !='') {
			hash = hash.replace("#", "");
			var paramArray = hash.split("&");
			$.each(paramArray, function() {
				var keyVal = this.split("=");
				if (keyVal[0] == key) {
					val = keyVal[1];
				}
			});
		}
		return val;
	};
	
	$.setHash = function(key, val) {
		var hash = window.location.hash;
		var url = window.location.href;
		if (hash && hash !='') {
			url = url.replace(hash, "");
		}
		url += "#" + key + "=" + val;
		window.location.href = url;
	};
	
	$.showInfo = function(msg) {
		$.gritter.add({
		    title: "提示",
		    text: msg,
		    time: '2000',
		    class_name: 'gritter-info gritter-light gritter-center'
		});
	};

	$.showErr = function(msg) {
		$.gritter.add({
		    title: "错误",
		    text: msg,
		    sticky: true,
		    time: '3000',
		    class_name: 'gritter-error gritter-center'
		});
	};
	
	$.showLoading = function(target) {
		if (!target) target = $(body);
		var loading = '<div class="idoc-loading"><div class="idoc-loading-over"></div><i class="idoc-loading-icon ace-icon fa fa-spinner fa-spin blue"></i></div>';
		target.append(loading);
	};
	
	$.hideLoading = function(target) {
		if (!target) target = $(body);
		target.find(".idoc-loading").remove();
	};
	
	$.formatDate = function(date) {
		if (typeof(date) == "string") {
			date = new Date(date);
		}
		var year = date.getFullYear();
		var month = (1 + date.getMonth()).toString();
		month = month.length > 1 ? month : '0' + month;
		var day = date.getDate().toString();
		day = day.length > 1 ? day : '0' + day;
		return year + '-' + month + '-' + day;
	};
	
	$.formatDatetime = function(date) {
		if (typeof(date) == "string") {
			date = new Date(date);
		}
		var year = date.getFullYear();
		var month = (1 + date.getMonth()).toString();
		month = month.length > 1 ? month : '0' + month;
		var day = date.getDate().toString();
		day = day.length > 1 ? day : '0' + day;
		var hour = date.getHours().toString();
		hour = hour.length > 1 ? hour : '0' + hour;
		var minute = date.getMinutes().toString();
		minute = minute.length > 1 ? minute : '0' + minute;
		var second = date.getSeconds().toString();
		second = second.length > 1 ? second : '0' + second;
		return year + '-' + month + '-' + day + " " + hour + ":" + minute + ":" + second;
	};
	
	$.isIe = function() {
		return ("ActiveXObject" in window);
	};
	
	$.isSupportH5 = function() {
		return window.applicationCache;
	};
	
})(jQuery);
